import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/widgets/show_datetime_picker.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class ExpirationDate extends StatefulWidget {
  const ExpirationDate({super.key});

  @override
  State<StatefulWidget> createState() => _ExpirationDateState();

}

class _ExpirationDateState extends State<ExpirationDate> {
  final Map _params = {
    "startTime": null,
    "endTime": null,
  };
  final Map _result = {
    "startTime": DateTime.now(),
    "endTime": DateTime.now(),
  };

  bool _canEdit() {
    return _result["startTime"] != _params["startTime"] || _result["endTime"] != _params["endTime"];
  }

  @override
  void initState() {

    setState(() {
      _params["startTime"] = _result["startTime"];
      _params["endTime"] = _result["endTime"];
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("修改有效期", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
              borderRadius: const BorderRadius.all(Radius.zero),
              items: [
                MenuBlockItem(
                  text: "生效时间",
                  suffix: Text(DateTools.formatTime(_params["startTime"].millisecondsSinceEpoch, format: "{y}-{m}-{d} {h}:{i}"), style: const TextStyle(color: ThemeVars.colorTextTer),),
                  onTap: () {
                    showDatetimePicker(
                      context: context,
                      initialDateTime: _params["startTime"],
                      minimumDate: _result["startTime"],
                      onchange: (value) => setState(() {
                        _params["startTime"] = value;
                      }),
                    );
                  },
                ),
                MenuBlockItem(
                  text: "失效时间",
                  suffix: Text(DateTools.formatTime(_params["endTime"].millisecondsSinceEpoch, format: "{y}-{m}-{d} {h}:{i}"), style: const TextStyle(color: ThemeVars.colorTextTer),),
                  onTap: () {
                    showDatetimePicker(
                      context: context,
                      minimumDate: _result["startTime"],
                      initialDateTime: _params["endTime"],
                      onchange: (value) => setState(() {
                        _params["endTime"] = value;
                      }),
                    );
                  },
                ),
              ]
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, 60, ThemeVars.paddingPage, 0),
            child: CupertinoButton(
              onPressed: _canEdit() ? () {
                print("确定");
              } : null,
              color: ThemeVars.colorPrimary,
              disabledColor: ThemeVars.colorDisabled,
              child: const Text("确定", style: TextStyle(color: Colors.white),),
            ),
          ),
        ],
      )
    );
  }

}